﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_client.csharp.model;
using System.Windows.Controls;
using mars_client.gui.page;
using System.Windows;
using mars_client.csharp.vo;
using System.Windows.Input;
using System.Windows.Media;
using mars_client.csharp.model.vo;
using mars_client.gui.window;
using System.Windows.Media.Imaging;
using System.IO;
using mars_client.csharp.enums;

namespace mars_client.csharp.util
{
    class RenderUtil
    {
        // 生成聊天内容模板
        public static Grid generateChatTemp(SingleChatVO singleChatVO,string orientation)
        {
            Grid g = new Grid();
            g.HorizontalAlignment = orientation == "right" ? HorizontalAlignment.Right : HorizontalAlignment.Left;
            g.FlowDirection = orientation == "right" ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
            g.Margin = new Thickness(0,10,0,10);
            RowDefinition row1 = new RowDefinition(); row1.Height = new GridLength(0);
            RowDefinition row2 = new RowDefinition(); row2.Height = new GridLength(30);
            RowDefinition row3 = new RowDefinition(); row3.Height = GridLength.Auto;
            g.RowDefinitions.Add(row1);
            g.RowDefinitions.Add(row2);
            g.RowDefinitions.Add(row3);
            ColumnDefinition col1 = new ColumnDefinition();col1.Width = new GridLength(50);
            ColumnDefinition col2 = new ColumnDefinition();col2.Width = new GridLength(350);
            g.ColumnDefinitions.Add(col1);
            g.ColumnDefinitions.Add(col2);
            

            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            sp.Margin = new Thickness(0, 0, 0, 6);
            TextBox tb1 = new TextBox();
            tb1.BorderThickness = new Thickness(0);
            tb1.Foreground = Brushes.Black;
            tb1.IsReadOnly = true;
            tb1.FontSize = 11;
            tb1.FontFamily = new FontFamily("宋体");
            tb1.Foreground = Brushes.Gray;
            tb1.VerticalContentAlignment = VerticalAlignment.Bottom;
            tb1.Margin = new Thickness(0, 0, 10, 0);
            tb1.Text = singleChatVO.sendTime;
            TextBox tb2 = new TextBox();
            tb2.BorderThickness = new Thickness(0);
            tb2.Foreground = Brushes.Black;
            tb2.IsReadOnly = true;
            tb2.FontSize = 11;
            tb2.FontFamily = new FontFamily("宋体");
            tb2.Foreground = Brushes.Gray;
            tb2.VerticalContentAlignment = VerticalAlignment.Bottom;
            tb2.Text = singleChatVO.senderVO.nickName;
            sp.Children.Add(tb1);
            sp.Children.Add(tb2);

            Image img = new Image();
            img.Height = 40;
            img.Width = 40;
            img.Source = singleChatVO.senderVO.headImg != null ? SysUtil.netImageSource(singleChatVO.senderVO.headImg) : SysUtil.generateImgSource(@"\resource\img\head\tx.png");
            img.VerticalAlignment = VerticalAlignment.Bottom;
            EllipseGeometry e = new EllipseGeometry();
            e.RadiusX = 20;
            e.RadiusY = 20;
            e.Center = new Point(20, 20);
            img.Clip = e;

            StackPanel sp2 = new StackPanel();
            sp2.Orientation = Orientation.Horizontal;
            sp2.VerticalAlignment = VerticalAlignment.Top;
            Border b = new Border();
            b.BorderThickness = new Thickness(1);
            b.BorderBrush = orientation == "right" ? (Brush)new BrushConverter().ConvertFromString("#38ADFF") : (Brush)new BrushConverter().ConvertFromString("#EFEFEF");
            b.CornerRadius = new CornerRadius(5);
            if(singleChatVO.messageVO.message == null)
            {
                Image img1 = new Image();
                img1.Width = 100;
                img1.Source = TransformUtil.Base64StringToImage(singleChatVO.messageVO.message);
                b.Child = img1;
            }
            else
            {
                TextBox tb3 = new TextBox();
                tb3.IsReadOnly = true;
                tb3.Padding = new Thickness(8, 8, 8, 8);
                tb3.VerticalAlignment = VerticalAlignment.Bottom;
                tb3.FontSize = 15;
                tb3.FontFamily = new FontFamily("宋体");
                tb3.BorderThickness = new Thickness(0);
                tb3.Background = orientation == "right" ? (Brush)new BrushConverter().ConvertFromString("#38ADFF") : Brushes.White;
                tb3.Foreground = orientation == "right" ? Brushes.White : Brushes.Black;
                tb3.Text = singleChatVO.messageVO.message;
                b.Child = tb3;
            }
            sp2.Children.Add(b);

            g.Children.Add(sp);
            g.Children.Add(img);
            g.Children.Add(sp2);

            Grid.SetRow(sp,1);
            Grid.SetColumn(sp, 1);
            Grid.SetRow(img, 3);
            Grid.SetColumn(img, 0);
            Grid.SetRow(sp2, 2);
            Grid.SetColumn(sp2, 1);

            return g;
        }
        // 生成朋友列表项模板
        public static StackPanel generateFriendTemp(UserVO vo,int index)
        {
            Brush b;
            if(index % 2 != 0)
            {
                b = (Brush)new BrushConverter().ConvertFromString("#EFEFEF");
            }else
            {
                b = (Brush)new BrushConverter().ConvertFromString("#fff");
            }
            StackPanel sp = new StackPanel();
            sp.Height = 70;
            sp.Uid = vo.id.ToString();
            sp.Orientation = Orientation.Horizontal;
            sp.VerticalAlignment = VerticalAlignment.Center;
            sp.MouseUp += new MyFriendsPage().MineFriendStackPanel_MouseUp;

            Image img = new Image();
            img.Height = 50;
            img.Margin = new Thickness(20,0,10,0);
            img.Source = SysUtil.netImageSource(vo.headImg);
            EllipseGeometry e = new EllipseGeometry();
            e.RadiusX = 25;
            e.RadiusY = 25;
            e.Center = new Point(25, 25);
            img.Clip = e;

            Label label = new Label();
            label.Content = vo.nickName;
            label.Foreground = vo.isOnline == 0 ? Brushes.OrangeRed : Brushes.Black;
            label.FontFamily = new FontFamily("Microsoft JhengHei");
            label.VerticalContentAlignment = VerticalAlignment.Center;

            Border border = new Border();
            border.HorizontalAlignment = HorizontalAlignment.Right;
            border.Width = 520;
            Image img2 = new Image();
            img2.Source = SysUtil.generateImgSource(@"\resource\img\ico\close.png");
            img2.Height = 13;
            img2.Width = 13;
            img2.Uid = vo.id.ToString();
            img2.HorizontalAlignment = HorizontalAlignment.Right;
            img2.VerticalAlignment = VerticalAlignment.Center;
            img2.Margin = new Thickness(0,0,20,0);
            img2.Cursor = Cursors.Hand;
            img2.MouseUp += new MyFriendsPage().deleleFriend;
            Panel.SetZIndex(border, 1000);
            Panel.SetZIndex(img2, 1000);
            border.Child = img2;


            sp.Children.Add(img);
            sp.Children.Add(label);
            StackPanel spp = new StackPanel();
            spp.Orientation = Orientation.Horizontal;
            spp.Background = b;
            spp.Children.Add(sp);
            spp.Children.Add(border);
            return spp;
        }
        // 生成群组列表项模板
        public static StackPanel generateGroupTemp(GroupVO vo, int index)
        {
            Brush b;
            if (index % 2 != 0)
            {
                b = (Brush)new BrushConverter().ConvertFromString("#EFEFEF");
            }
            else
            {
                b = (Brush)new BrushConverter().ConvertFromString("#fff");
            }
            StackPanel sp = new StackPanel();
            sp.Height = 70;
            sp.Background = b;
            sp.Uid = vo.id.ToString();
            sp.Orientation = Orientation.Horizontal;
            sp.VerticalAlignment = VerticalAlignment.Center;
            sp.MouseUp += new MyGroupsPage().MineGroupStackPanel_MouseUp;

            Image img = new Image();
            img.Height = 50;
            img.Margin = new Thickness(20, 0, 10, 0);
            img.Source = SysUtil.netImageSource("http://pic.qiantucdn.com/58pic/13/81/87/16F58PICUjV_1024.png");
            EllipseGeometry e = new EllipseGeometry();
            e.RadiusX = 25;
            e.RadiusY = 25;
            e.Center = new Point(25, 25);
            img.Clip = e;

            StackPanel sp2 = new StackPanel();
            sp2.Orientation = Orientation.Vertical;
            
            Label label = new Label();
            label.Content = vo.groupName;
            label.VerticalContentAlignment = VerticalAlignment.Bottom;
            label.FontFamily = new FontFamily("Microsoft JhengHei");
            label.FontSize = 13;


            Label label2 = new Label();
            label2.Content = vo.groupIntro;
            label2.VerticalAlignment = VerticalAlignment.Center;
            label2.FontFamily = new FontFamily("Microsoft JhengHei");
            label2.FontSize = 11;

            sp2.Children.Add(label);
            sp2.Children.Add(label2);

            sp.Children.Add(img);
            sp.Children.Add(sp2);
            return sp;
        }
        // 生成聊天列表模板
        public static Grid generateChatListItemTemp(ChatListItemVO vo)
        {
            ChatListPage clp = (ChatListPage)GuiCache.Instance.getGui("ChatListPage");
            Grid g = new Grid();
            g.HorizontalAlignment = HorizontalAlignment.Left;
            g.VerticalAlignment = VerticalAlignment.Center;
            g.Height = 55;
            g.Width = 210;
            g.Uid = vo.id;
            g.DataContext = vo.type;

            Image img1 = new Image();
            img1.Source = SysUtil.generateImgSource(@"\resource\img\ico\close.png");
            img1.VerticalAlignment = VerticalAlignment.Top;
            img1.HorizontalAlignment = HorizontalAlignment.Left;
            img1.Height = 10;
            img1.Width = 10;
            img1.Margin = new Thickness(2, 23, 0, 0);
            img1.Cursor = Cursors.Hand;
            img1.MouseUp += clp.Delete_MouseUp;

            Image img2 = new Image();
            img2.Source = vo.headImg != null ? SysUtil.netImageSource(vo.headImg) : SysUtil.generateImgSource(@"\resource\img\button\anonymous.png");
            img2.HorizontalAlignment = HorizontalAlignment.Right;
            EllipseGeometry e1 = new EllipseGeometry();
            e1.RadiusX = 18;
            e1.RadiusY = 18;
            e1.Center = new Point(18, 18);
            img2.Clip = e1;
            img2.Height = 36;
            img2.Width = 36;
            img2.Margin = new Thickness(0, 10, 161, 0);
            img2.VerticalAlignment = VerticalAlignment.Top;

            TextBox tb1 = new TextBox();
            tb1.Height = 17;
            tb1.Margin = new Thickness(54, 8, 38, 0);
            tb1.Text = vo.name;
            tb1.VerticalAlignment = VerticalAlignment.Top;
            tb1.Width = 117;
            tb1.FontSize = 12;
            tb1.FontFamily = new FontFamily("宋体");
            tb1.IsReadOnly = true;
            tb1.Background = Brushes.Transparent;
            tb1.BorderThickness = new Thickness(0);

            TextBox tb2 = new TextBox();
            tb2.Height = 17;
            tb2.Margin = new Thickness(55, 28, 38, 0);
            tb2.Text = vo.lastestMessage != null ? vo.lastestMessage : "";
            tb2.VerticalAlignment = VerticalAlignment.Top;
            tb2.Width = 117;
            tb2.FontSize = 12;
            tb2.Foreground = Brushes.Gray;
            tb2.FontFamily = new FontFamily("宋体");
            tb2.IsReadOnly = true;
            tb2.Background = Brushes.Transparent;
            tb2.BorderThickness = new Thickness(0);

            TextBox tb3 = new TextBox();
            tb3.Height = 17;
            tb3.Margin = new Thickness(171, 10, 0, 0);
            tb3.Text = vo.lastestTime != null ? "" : vo.lastestTime;
            tb3.VerticalAlignment = VerticalAlignment.Top;
            tb3.Width = 117;
            tb3.FontSize = 12;
            tb3.FontWeight = FontWeights.Light;
            tb3.Foreground = Brushes.Gray;
            tb3.FontFamily = new FontFamily("宋体");
            tb3.IsReadOnly = true;
            tb3.Background = Brushes.Transparent;
            tb3.BorderThickness = new Thickness(0);

            Label label = new Label();
            label.HorizontalAlignment = HorizontalAlignment.Right;
            label.Margin = new Thickness(0, 23, 0, 0);
            label.Height = 24;
            label.Content = ">";
            label.Foreground = (Brush)new BrushConverter().ConvertFromString("#3299CC");
            label.VerticalAlignment = VerticalAlignment.Top;
            label.Width = 20;
            label.FontSize = 12;
            label.FontFamily = new FontFamily("宋体");
            label.FontWeight = FontWeights.Bold;
            label.Background = Brushes.Transparent;
            label.BorderThickness = new Thickness(0);
            label.Cursor = Cursors.Hand;
            label.MouseUp += clp.Grid_MouseUp;

            Border border = new Border();
            border.BorderBrush = Brushes.LightGray;
            border.BorderThickness = new Thickness(1);
            border.HorizontalAlignment = HorizontalAlignment.Left;
            border.Height = 1;
            border.Margin = new Thickness(0, 54, 0, 0);
            border.VerticalAlignment = VerticalAlignment.Top;
            border.Width = 210;
            border.Background = (Brush)new BrushConverter().ConvertFromString("#F5F9FF");

            g.Children.Add(img1);
            g.Children.Add(img2);
            g.Children.Add(tb1);
            g.Children.Add(tb2);
            g.Children.Add(tb3);
            g.Children.Add(label);
            g.Children.Add(border);
            TextBox tb4 = new TextBox();
            EllipseGeometry e = new EllipseGeometry();
            e.RadiusX = 8;
            e.RadiusY = 8;
            e.Center = new Point(8, 8);
            tb4.Clip = e;
            tb4.Text = "0";
            tb4.Visibility = Visibility.Hidden;
            tb4.Height = 16;
            tb4.Width = 16;
            tb4.Foreground = (Brush)new BrushConverter().ConvertFrom("#fff");
            tb4.Background = (Brush)new BrushConverter().ConvertFrom("#f00");
            tb4.VerticalContentAlignment = VerticalAlignment.Center;
            tb4.HorizontalContentAlignment = HorizontalAlignment.Center;
            tb4.BorderThickness = new Thickness(0);
            tb4.Margin = new Thickness(0, -20, 123, 0);
            g.Children.Add(tb4);
            return g;
        }
        // 生成添加用户模板
        public static StackPanel generateUserTemp(UserVO userVO,string isVisible)
        {
            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            sp.VerticalAlignment = VerticalAlignment.Center;
            sp.Height = 60;

            Image img = new Image();
            img.Source = SysUtil.netImageSource(userVO.headImg);
            img.Height = 40;
            img.Width = 40;
            EllipseGeometry e = new EllipseGeometry();
            e.Center = new Point(20, 20);
            e.RadiusX = 20;
            e.RadiusY = 20;
            img.Clip = e;

            StackPanel sp1 = new StackPanel();
            sp1.Width = 140;
            sp1.VerticalAlignment = VerticalAlignment.Center;
            sp1.Margin = new Thickness(10, 0, 0, 0);

            Label label = new Label();
            label.Content = userVO.nickName;
            label.FontSize = 14;
            label.FontFamily = new FontFamily("微软雅黑");

            StackPanel sp2 = new StackPanel();
            sp2.Orientation = Orientation.Horizontal;
            Label label1 = new Label();
            label1.FontSize = 11;
            label1.FontFamily = new FontFamily("微软雅黑");
            label1.Foreground = Brushes.Gray;
            label1.Width = 30;
            label1.Content = userVO.sex == (int)RequestEnum.sex.male ? "男士" :　"女士";
            Label label2 = new Label();
            label2.FontSize = 11;
            label2.FontFamily = new FontFamily("微软雅黑");
            label2.Foreground = Brushes.Gray;
            label2.Content = userVO.userIntro;
            sp2.Children.Add(label1);
            sp2.Children.Add(label2);

            sp1.Children.Add(label);
            sp1.Children.Add(sp2);

            Image img1 = new Image();
            img1.Height = 20;
            img1.Uid = userVO.id.ToString();
            img1.Cursor = Cursors.Hand;
            img1.Source = SysUtil.generateImgSource(@"\resource\img\ico\plus.png");
            img1.MouseUp += new UsersWindow().Image_MouseUp;
            img1.Visibility = isVisible == "visible" ? Visibility.Visible : Visibility.Hidden;

            sp.Children.Add(img);
            sp.Children.Add(sp1);
            sp.Children.Add(img1);
            return sp;
        }
        // 生成创建群列表模板
        public static StackPanel generateUserForGroupTemp(UserVO userVO)
        {
            StackPanel sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;
            sp.VerticalAlignment = VerticalAlignment.Center;
            sp.Height = 60;
            sp.Background = Brushes.White;
            sp.Uid = userVO.id.ToString();
            sp.MouseUp += new CreateGroupWindow().select;

            Image img = new Image();
            img.Source = SysUtil.netImageSource(userVO.headImg);
            img.Height = 40;
            img.Width = 40;
            img.Margin = new Thickness(15,0,0,0);
            EllipseGeometry e = new EllipseGeometry();
            e.Center = new Point(20, 20);
            e.RadiusX = 20;
            e.RadiusY = 20;
            img.Clip = e;

            StackPanel sp1 = new StackPanel();
            sp1.Width = 140;
            sp1.VerticalAlignment = VerticalAlignment.Center;
            sp1.Margin = new Thickness(10, 0, 0, 0);

            Label label = new Label();
            label.Content = userVO.nickName;
            label.Foreground = userVO.isOnline == 0 ? Brushes.OrangeRed : Brushes.Black;
            label.FontSize = 14;
            label.FontFamily = new FontFamily("微软雅黑");

            StackPanel sp2 = new StackPanel();
            sp2.Orientation = Orientation.Horizontal;
            Label label1 = new Label();
            label1.FontSize = 11;
            label1.FontFamily = new FontFamily("微软雅黑");
            label1.Foreground = Brushes.Gray;
            label1.Width = 30;
            label1.Content = userVO.sex == (int)RequestEnum.sex.male ? "男" : "女";
            Label label2 = new Label();
            label2.FontSize = 11;
            label2.FontFamily = new FontFamily("微软雅黑");
            label2.Foreground = Brushes.Gray;
            label2.Content = userVO.userIntro;
            sp2.Children.Add(label1);
            sp2.Children.Add(label2);

            sp1.Children.Add(label);
            sp1.Children.Add(sp2);

            sp.Children.Add(img);
            sp.Children.Add(sp1);
            return sp;
        }
    }
}
